package com.ezjie.ielts.util;

import java.io.File;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.os.storage.StorageManager;
import android.text.TextUtils;

/**
 * sdcard操作工具类
 * 
 * @author renyangyang
 *
 */
public class SDcardUtil {

	/**
	 * 判断是否插入SD卡，SD卡是否可用
	 * 
	 * @return
	 */
	public static boolean hasSdcard() {
		String status = Environment.getExternalStorageState();
		if (status.equals(Environment.MEDIA_MOUNTED) && Environment.getExternalStorageDirectory().canWrite()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 文件目录：如sensecp/pic
	 * 
	 * @param fileDir
	 * @return
	 */
	public static String getSDPath(String fileDir) {
		File sdcardDir = Environment.getExternalStorageDirectory();
		String path = sdcardDir.getParent() + "/" + sdcardDir.getName();
		final String fileDirectory = path + "/" + fileDir;
		return fileDirectory;
	}

	/**
	 * sdcard上是否有此文件
	 * 
	 * @param fileDir
	 * @param fileName
	 * @return
	 */
	public static boolean isFileOnSDCard(String fileDir, String fileName) {
		/**
		 * 如果连SD卡都没有就别提还存文件了
		 */
		if (!hasSdcard()) {
			return false;
		}

		String fileDirectory = getSDPath(fileDir);
		File file = new File(fileDirectory, fileName);
		if (file.exists()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获取内部存储剩余空间
	 * 
	 * @return
	 */
	public static long getAvailableInternalMemorySize() {
		File path = Environment.getDataDirectory(); // 获取数据目录
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;
	}

	/**
	 * 获取内部存储总空间
	 * 
	 * @return
	 */
	public static long getTotalInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long totalBlocks = stat.getBlockCount();
		return totalBlocks * blockSize;
	}

	/**
	 * 得到外部存储剩余空间(MB)
	 * 
	 * @return
	 */
	public static long getAvailableExternalMemorySize() {
		if (hasSdcard()) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long availableBlocks = stat.getAvailableBlocks();
			return availableBlocks * blockSize;
		} else {
			return -1;
		}
	}

	/**
	 * 得到外部存储总空间(MB)
	 * 
	 * @return
	 */
	public static long getTotalExternalMemorySize() {
		if (hasSdcard()) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long totalBlocks = stat.getBlockCount();
			return totalBlocks * blockSize;
		} else {
			return -1;
		}
	}

	/**
	 * 根据路径获得 可用空间(MB)
	 * 
	 * @param filePath
	 * @return
	 */
	public static long getAvailableSizeByPath(String filePath) {
		StatFs stat = new StatFs(filePath);
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;
	}

	/**
	 * 根据路径获得 总空间(MB)
	 * 
	 * @param filePath
	 * @return
	 */
	public static long getTotalSizeByPath(String filePath) {
		StatFs stat = new StatFs(filePath);
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getBlockCount();
		return availableBlocks * blockSize;
	}

	/**
	 * 获取所有存储路径
	 * 
	 * @param context
	 * @return
	 */
	@SuppressLint("InlinedApi")
	public static String[] getStoragePaths(Context context) {
		try {
			StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
			String[] paths = (String[]) storageManager.getClass().getMethod("getVolumePaths", null)
					.invoke(storageManager);
			ArrayList<String> list = new ArrayList<String>();
			for (String path : paths) {
				String state = (String) storageManager.getClass().getMethod("getVolumeState", String.class)
						.invoke(storageManager, path);
				if (TextUtils.equals("mounted", state))
					list.add(path);
			}
			return list.toArray(new String[0]);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static final String DOWNLOAD_FILENAME = "easyjie_ielts_Listen";

	public static File setDestinationInExternalPublicDir() {
		File file = Environment.getExternalStoragePublicDirectory(DOWNLOAD_FILENAME);
		if (file == null) {
			throw new IllegalStateException("Failed to get external storage public directory");
		} else if (file.exists()) {
			if (!file.isDirectory()) {
				throw new IllegalStateException(file.getAbsolutePath() + " already exists and is not a directory");
			}
		} else {
			if (!file.mkdirs()) {
				throw new IllegalStateException("Unable to create directory: " + file.getAbsolutePath());
			}
		}
		return file;
	}

}
